package com.ce.pms.charge.web;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;



import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.charge.model.PeriodDomain;
import com.ce.pms.charge.service.IChargeVoucherService;
import com.ce.pms.charge.service.IPeriodService;
import com.ce.pms.charge.vo.ChargeVoucherSearchVO;
import com.ce.pms.charge.vo.ChargeVoucherVO;
import com.ce.pms.charge.vo.DebitTransVO;
import com.ce.pms.charge.vo.PeriodVO;
import com.ce.pms.charge.vo.TransactionVO;
import com.ce.pms.charge.vo.VoucherPayMethodVO;
import com.ce.pms.common.service.ISysrefcodeService;
import com.ce.pms.house.service.IBuildingService;
import com.ce.pms.house.vo.BuildingVO;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.components.xtable.util.ExtremeUtils;
import com.sitechasia.webx.core.annotation.Read;
import com.sitechasia.webx.core.support.Page;


/**
 * 功能描述：收费凭证打印<br>
 *
 * @author 黄彦军 创建日期：2008-8-13
 *
 */
public class ChargeVoucherPrintAction extends PmsBaseAction {

	private IChargeVoucherService chargeVoucherService;
	private IBuildingService buildingService;
	private ISysrefcodeService sysrefcodeService;
	private IPeriodService periodService;


	/**
	 * 功能描述:加载收费凭证打印主页面 <br>
	 *
	 * Create author:黄彦军 <br>
	 * Create on:2008-8-13 <br>
	 * Edit author: <br>
	 * Edit on: <br>
	 * Why: <br>
	 *
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String initChargeVoucherMain(@Read ChargeVoucherSearchVO searchVO, @Read(key = "ec_crd",defaultValue="10")	int pageSize,
            @Read(key = "ec_p", defaultValue="1")	int pageNo) {

		//获取缴费方式列表
		List payMethods = sysrefcodeService.querySysrefCodesByCode("FEE_BUSINESS_CODE_PAY_METHOD");
		//获取凭证状态列表
		List statuzs = sysrefcodeService.querySysrefCodesByCode("FEE_VOUCHER_STATUZ");
		//获取所有楼栋
		List<BuildingVO> buildings = buildingService.getAllBuildingList();
		//获取所有收费周期
		List<PeriodVO> periods = periodService.findPeriodByValidity(null);

		this.getRequest().setAttribute("payMethods", payMethods);
		this.getRequest().setAttribute("statuzs", statuzs);
		this.getRequest().setAttribute("periods", periods);
		this.getRequest().setAttribute("buildings", buildings);

		//如果是第一次请求，则不加载凭证信息
		String isfirst = this.getRequest().getParameter("isfirst");
		if ("true".equals(isfirst)){
			this.getRequest().setAttribute("list", null);
			this.getRequest().setAttribute("totalRows",0);
			this.getRequest().setAttribute("currentlyPage", 1);

			return "chargeVoucherMain";
		}

		//如果不是第一次加载则执行后面的代码－－－－－－－－－－－－－－－－－－－－

		//加载排序参数
		String sortStr = ExtremeUtils.getOrderString(getRequest());
		if (!BlankUtil.isBlank(sortStr)) {
			if (sortStr.equals("chargeDate asc")) {
				searchVO.setSortStr("vou.CHARGE_DATE asc");
			} else if (sortStr.equals("chargeDate desc")) {
				searchVO.setSortStr("vou.CHARGE_DATE  desc");
			} else if (sortStr.equals("houseNumber asc")) {
				searchVO.setSortStr("hmh.HOUSE_NUMBER  asc");
			} else if (sortStr.equals("houseNumber desc")) {
				searchVO.setSortStr("hmh.HOUSE_NUMBER  desc");
			}
		} else {
			searchVO.setSortStr("vou.CHARGE_DATE  desc");
		}

		//获取凭证信息列表
		searchVO.setPageNo(pageNo);
		searchVO.setPageSize(pageSize);

		Page page = chargeVoucherService.queryCurrentlyPageVoucher(searchVO);
		List<ChargeVoucherVO> list = page.getResult();

		if (list == null || list.size() == 0){
			this.getRequest().setAttribute("list", null);
		}else{
			this.getRequest().setAttribute("list", list);
		}

		Integer currentlyPage = page.getCurrentPageNo();//当前页
		Long totalCounts = page.getTotalCount();

		this.getRequest().setAttribute("searchVO", searchVO);//凭证信息列表
		this.getRequest().setAttribute("currentlyPage", currentlyPage);//当前页
		this.getRequest().setAttribute("totalRows",totalCounts.intValue());//总行数
		this.getRequest().setAttribute("pageSize",pageSize);//页大小

		return "chargeVoucherMain";
	}

	/**
	 * 功能描述:加载收费凭证详细页面 <br>
	 *
	 * Create author:黄彦军 <br>
	 * Create on:2008-8-13 <br>
	 * Edit author: <br>
	 * Edit on: <br>
	 * Why: <br>
	 *
	 * @return
	 */
	public String initChargeVoucherDetail(@Read (key = "voucherId")Long voucherId) {
		List list =chargeVoucherService.getPayListByVoucherId(voucherId);
		String paySummary = chargeVoucherService.getpaySummaryByVoucherId(voucherId);
		getRequest().setAttribute("voucherId", voucherId);
		getRequest().setAttribute("list", list);
		getRequest().setAttribute("paySummary", paySummary);
		return "chargeVoucherDetail";
	}

	/**
	 * 功能描述: 加载弹出收费流程模态页面 <br>
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-8-13  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String chargeCycleForwardSmall(){
		return "chargeCycleForwardSmall";
	}

	/**
	 * 功能描述:加载支付明细页面   <br>
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-8-22  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String payDetailList(@Read (key = "voucherId") Long voucherId){
		String sortStr = ExtremeUtils.getOrderString(getRequest());
		List<VoucherPayMethodVO> list = chargeVoucherService.getpayTrans(voucherId,sortStr);
		Double sum = new Double(0);
		if (!BlankUtil.isBlank(list)){
			for (VoucherPayMethodVO methodVO : list) {
				BigDecimal b1 = new BigDecimal(sum.toString());
				BigDecimal b2 = new BigDecimal(methodVO.getPrice().toString());
				sum = b1.add(b2).doubleValue();
			}
		}
		setAttribute("sum",sum );
		setAttribute("payBillList", list);
		setAttribute("totalRows", new Integer(list.size()<3?3:list.size()));
		setAttribute("voucherId", voucherId);
		return "payDetailList";
	}


	public void updateVoucherStart(@Read (key = "voucherId") Long voucherId){
		chargeVoucherService.updateVoucherStart(voucherId);
	}

	//-------------------------------------------------------------------

	public void setBuildingService(IBuildingService buildingService) {
		this.buildingService = buildingService;
	}

	public IBuildingService getBuildingService() {
		return buildingService;
	}
	public IChargeVoucherService getChargeVoucherService() {
		return chargeVoucherService;
	}

	public void setChargeVoucherService(IChargeVoucherService chargeVoucherService) {
		this.chargeVoucherService = chargeVoucherService;
	}

	public ISysrefcodeService getSysrefcodeService() {
		return sysrefcodeService;
	}

	public void setSysrefcodeService(ISysrefcodeService sysrefcodeService) {
		this.sysrefcodeService = sysrefcodeService;
	}

	public IPeriodService getPeriodService() {
		return periodService;
	}

	public void setPeriodService(IPeriodService periodService) {
		this.periodService = periodService;
	}





}


